package com.gexiaobao.pigeon.ui.fragment.mine.organization

import android.annotation.SuppressLint
import android.os.Bundle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.databinding.FragmentOrganizationBinding
import com.gexiaobao.pigeon.viewmodel.OrganizationViewModel
import com.google.android.material.tabs.TabLayout
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 15:09
 * @Describe    : 我的组织、俱乐部、协会
 */
class FragmentOrganization : BaseFragment<OrganizationViewModel, FragmentOrganizationBinding>() {

    private var status: Int = 0
    private var title = ""

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        status = requireArguments().getInt("status")
        title = when (status) {
            1 -> getString(R.string.club)
            2 -> getString(R.string.association)
            else -> getString(R.string.shanty)

        }
        mDatabind.toolbar.initClose(getString(R.string.main_me) + title) {
            nav().navigateUp()
        }
        mDatabind.tvSearchOrganization.text = getString(R.string.activity_market_shop_search_result_search) + title
    }

    override fun initData() {
        super.initData()
        initViewPager()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvSearchOrganization) {
            when (it) {
                mDatabind.tvSearchOrganization -> {
                    nav().navigateAction(R.id.action_to_search_organization, Bundle().apply {
                        putInt("status", status)
                        putString("title", title)
                    })
                }
            }
        }
    }

    private fun initViewPager() {
        val tab1 = mDatabind.tabLayout.newTab().setText(getString(R.string.I_added))
        mDatabind.tabLayout.addTab(tab1)
        val tab2 = mDatabind.tabLayout.newTab().setText(getString(R.string.My_concern))
        mDatabind.tabLayout.addTab(tab2)
        val tab3 = mDatabind.tabLayout.newTab().setText(getString(R.string.all) + title)
        mDatabind.tabLayout.addTab(tab3)
        mDatabind.tabLayout.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<TabLayout.Tab> {
            override fun onTabReselected(p0: TabLayout.Tab) {}
            override fun onTabUnselected(p0: TabLayout.Tab) {}

            override fun onTabSelected(p0: TabLayout.Tab) {
                replaceFragments(p0.position)
            }
        })
        replaceFragments(0)
    }

    private fun replaceFragments(position: Int) {
        childFragmentManager.beginTransaction().apply {
            when (position) {
                0 -> replace(R.id.orgContainer, FragmentJoin.newInstance(status))
                1 -> replace(R.id.orgContainer, FragmentCare.newInstance(status))
                2 -> replace(R.id.orgContainer, FragmentAllOrg.newInstance(status))
            }
        }.commitAllowingStateLoss()
    }

}